package zaijiansunwukong.util;

import org.apache.commons.io.IOUtils;

import java.io.*;

/**
 * Created by wzy-pc on 2016/5/17.
 */
public class FileUtil {
    /**
     * 获取文件的byte[]数据
     *
     * @param path
     * @return
     * @throws java.io.FileNotFoundException,java.io.IOException
     */
    public static byte[] localBufferRead(String path) throws FileNotFoundException, IOException {
        File file = new File(path);
        FileInputStream inputFile = null;
        byte[] buffer = null;
        try {
            inputFile = new FileInputStream(file);
            buffer = new byte[(int) file.length()];
            inputFile.read(buffer);
        } catch (IOException e) {
            //logger.error("本地缓存文件读取失败：" + path);
            throw e;
        } finally {
            if (inputFile != null) {
                try {
                    inputFile.close();
                } catch (IOException e) {
                    throw e;
                }
            }
        }
        return buffer;
    }

    /**
     * 将二进制文件数据写入目标路径
     *
     * @param buffer
     * @param targetPath
     * @throws java.io.IOException
     */
    public static void localBufferWrite(byte[] buffer, String targetPath) throws IOException {
        File file = new File(targetPath);
        File pathFile = file.getParentFile();
        pathFile.mkdirs();
        file.createNewFile();
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(targetPath);
//            out.write(buffer);
//            file.setWritable(true, false);
            IOUtils.write(buffer, out);
        } catch (IOException e) {
            throw e;
        } finally {
            IOUtils.closeQuietly(out);
        }
    }

    /**
     * 支持关闭流,目前支持关闭：
     * io:FileInputStream,FileOutputStream,ByteArrayOutputStream,OutputStream,
     * InputStream,InputStreamReader,BufferedReader
     */
    public static void closeStream(Object obj) {
        try {
            if (obj != null) {
                if (obj instanceof FileInputStream) {
                    ((FileInputStream) obj).close();
                } else if (obj instanceof FileOutputStream) {
                    ((FileOutputStream) obj).close();
                } else if (obj instanceof ByteArrayOutputStream) {
                    ((ByteArrayOutputStream) obj).close();
                } else if (obj instanceof OutputStream) {
                    ((OutputStream) obj).close();
                } else if (obj instanceof InputStream) {
                    ((InputStream) obj).close();
                } else if (obj instanceof InputStreamReader) {
                    ((InputStreamReader) obj).close();
                } else if (obj instanceof BufferedReader) {
                    ((BufferedReader) obj).close();
                }

            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }


    /**
     * 判断文件是否有访问权限
     * @param file
     */
    public static boolean isReadable(File file) {
        FileInputStream fis = null;
        try{
            fis = new FileInputStream(file);
            if (fis.read() != -1) {
                return true;
            }
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return  false;
    }
}
